package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.Recruitment;
import com.ruoyi.system.domain.Resume;
import com.ruoyi.system.mapper.RecruitmentMapper;
import com.ruoyi.system.mapper.ResumeMapper;
import com.ruoyi.system.model.SendRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.RecruitmentResumeMapper;
import com.ruoyi.system.domain.RecruitmentResume;
import com.ruoyi.system.service.IRecruitmentResumeService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 招聘信息-简历Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-05
 */
@Service
public class RecruitmentResumeServiceImpl implements IRecruitmentResumeService
{
    @Resource
    private RecruitmentResumeMapper recruitmentResumeMapper;

    @Resource
    private ResumeMapper resumeMapper;

    @Resource
    private RecruitmentMapper recruitmentMapper;

    /**
     * 查询招聘信息-简历
     *
     * @param recResId 招聘信息-简历主键
     * @return 招聘信息-简历
     */
    @Override
    public RecruitmentResume selectRecruitmentResumeByRecResId(Long recResId)
    {
        return recruitmentResumeMapper.selectRecruitmentResumeByRecResId(recResId);
    }

    /**
     * 查询招聘信息-简历列表
     *
     * @param recruitmentResume 招聘信息-简历
     * @return 招聘信息-简历
     */
    @Override
    public List<RecruitmentResume> selectRecruitmentResumeList(RecruitmentResume recruitmentResume)
    {
        return recruitmentResumeMapper.selectRecruitmentResumeList(recruitmentResume);
    }

    /**
     * 新增招聘信息-简历
     *
     * @param recruitmentResume 招聘信息-简历
     * @return 结果
     */
    @Override
    public int insertRecruitmentResume(RecruitmentResume recruitmentResume)
    {
        return recruitmentResumeMapper.insertRecruitmentResume(recruitmentResume);
    }

    /**
     * 修改招聘信息-简历
     *
     * @param recruitmentResume 招聘信息-简历
     * @return 结果
     */
    @Override
    public int updateRecruitmentResume(RecruitmentResume recruitmentResume)
    {
        return recruitmentResumeMapper.updateRecruitmentResume(recruitmentResume);
    }

    /**
     * 批量删除招聘信息-简历
     *
     * @param recResIds 需要删除的招聘信息-简历主键
     * @return 结果
     */
    @Override
    public int deleteRecruitmentResumeByRecResIds(String recResIds)
    {
        return recruitmentResumeMapper.deleteRecruitmentResumeByRecResIds(Convert.toStrArray(recResIds));
    }

    /**
     * 删除招聘信息-简历信息
     *
     * @param recResId 招聘信息-简历主键
     * @return 结果
     */
    @Override
    public int deleteRecruitmentResumeByRecResId(Long recResId)
    {
        return recruitmentResumeMapper.deleteRecruitmentResumeByRecResId(recResId);
    }

    @Override
    public List<SendRecord> selectSendRecord(RecruitmentResume recruitmentResume) {
        //应聘者投递简历的招聘信息
        List<SendRecord> sendRecordList = new ArrayList<>();
        SysUser sysUser = ShiroUtils.getSysUser();
        List<Resume> resumeList = resumeMapper.selectResumeByResumeName(sysUser.getUserName());
        if (resumeList.size() > 0) {
            for (Resume resume : resumeList) {
                recruitmentResume.setResumeId(resume.getResumeId());
                recruitmentResume.setFlag(0);
                List<RecruitmentResume> recruitmentResumes = recruitmentResumeMapper.selectRecruitmentResumeList(recruitmentResume);
                if (recruitmentResumes.size() > 0) {
                    for (RecruitmentResume recRes : recruitmentResumes) {
                        SendRecord sendRecord = new SendRecord();
                        sendRecord.setRecResId(recRes.getRecResId());
                        Recruitment recruitment = recruitmentMapper.selectRecruitmentByRecruitmentId(recRes.getRecruitmentId());
                        sendRecord.setRecruitmentName(recruitment.getRecruitmentName());
                        sendRecord.setCompanyName(recruitment.getRecruitmentCompany());
                        RecruitmentResume recruRes = new RecruitmentResume();
                        recruRes.setResumeId(recRes.getResumeId());
                        recruRes.setRecruitmentId(recRes.getRecruitmentId());
                        recruRes.setFlag(1);
                        List<RecruitmentResume> recruitmentResumes1 = recruitmentResumeMapper.selectRecruitmentResumeList(recruRes);
                        sendRecord.setIsAccept(recruitmentResumes1.size() > 0);
                        sendRecordList.add(sendRecord);
                    }
                }
            }
        }
        return sendRecordList;
    }

    @Override
    public Boolean giveRecruitment(String recResId) {
        RecruitmentResume recruitmentResume = recruitmentResumeMapper.selectRecruitmentResumeByRecResId(Long.valueOf(recResId));
        recruitmentResume.setFlag(1);
        return recruitmentResumeMapper.insertRecruitmentResume(recruitmentResume) > 0;
    }
}
